﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using global::TerhelesEloszto.Common;
using global::TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class User : System.Web.UI.Page
	{
		protected global::TerhelesEloszto.Engine.IEngine engine = global::TerhelesEloszto.Engine.Engine.Instance();
		protected void Page_Load( object sender, EventArgs e )
		{
			String visitorName = User.Identity.Name;
			if( !engine.IsAdmin( visitorName ) )
			{
				if( engine.IsTeacher( visitorName ) )
				{
					Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}

			}
			if( Request.QueryString["msg"] != null )
			{
				if( Request.QueryString["msg"].Equals( "1" ) ) MessageLabel.Text = "A műveletet sikeresen végrehajtottuk!";
				else if( Request.QueryString["msg"].Equals( "0" ) ) MessageLabel.Text = "Hiba történt a művelet végrehajtása közben";
			}
			try
			{
				loadTable();
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Admin/Error.aspx" );
			}

		}
		public void loadTable()
		{


			TableCell tCellHeader1 = new TableCell();
			tCellHeader1.Text = "Neptun";
			TableRow tRow = new TableRow();
			tRow.Cells.Add( tCellHeader1 );
			TableUser.Rows.Add( tRow );

			TableCell tCellHeader2 = new TableCell();
			tCellHeader2.Text = "Név";

			tRow.Cells.Add( tCellHeader2 );
			TableUser.Rows.Add( tRow );

			TableCell tCellHeader3 = new TableCell();
			tCellHeader3.Text = "E-mail";
			tRow.Cells.Add( tCellHeader3 );

			TableCell tCellHeader4 = new TableCell();
			tCellHeader4.Text = "Szerep";
			tRow.Cells.Add( tCellHeader4 );

			TableCell tCellHeader5 = new TableCell();
			tCellHeader5.Text = "";
			tRow.Cells.Add( tCellHeader5 );

			TableCell tCellHeader6 = new TableCell();
			tCellHeader6.Text = "";
			tRow.Cells.Add( tCellHeader6 );

			TableUser.GridLines = GridLines.Both;
			TableUser.BorderStyle = BorderStyle.Groove;
			TableUser.CellPadding = 15;
			tRow.CssClass = "bold";
			TableUser.Rows.Add( tRow );

			int i = 0;
			foreach( IUser actUser in engine.GetUsers() )
			{
				++i;
				TableRow actRow = new TableRow();

				TableCell actc1 = new TableCell();
				actc1.Text = actUser.Neptun;
				actRow.Cells.Add( actc1 );

				TableCell actc2 = new TableCell();
				actc2.Text = actUser.Name;
				actRow.Cells.Add( actc2 );


				TableCell actc3 = new TableCell();
				actc3.Text = actUser.Email;
				actRow.Cells.Add( actc3 );

				TableCell actc4 = new TableCell();


				if( engine.IsAdmin( actUser.Neptun ) && engine.IsTeacher( actUser.Neptun ) )
				{
					actc4.Text = "Oktató <br/> Admin";
				}
				else if( engine.IsTeacher( actUser.Neptun ) )
				{
					actc4.Text = "Oktató";
				}
				else if( engine.IsAdmin( actUser.Neptun ) )
				{
					actc4.Text = "Admin";
				}
				actRow.Cells.Add( actc4 );
				TableCell actc5 = new TableCell();
				actc5.Text = "<a href=\"ModifyUser.aspx?ID=" + actUser.Neptun + "\">Módosítás<a>";
				actRow.Cells.Add( actc5 );
				TableCell actc6 = new TableCell();
				actc6.Text = "<a href=\"YesNo.aspx?ID=" + actUser.Neptun.Trim() + "&T=userdelete&Q=Biztosan+törli+a+felhasználót\">Eltávolítás<a>";
				actRow.Cells.Add( actc6 );
				//TableUser.Font.Name = "Arial";
				//TableUser.Font.Bold = true;
				TableUser.Rows.Add( actRow );
			}
		}

		protected void Button1_Click( object sender, EventArgs e )
		{
			Response.Redirect( "~/Admin/CreateUser.aspx" );
		}
	}
}